//
//  JHJTabBarCollectionViewCell.m
//  项目
//
//  Created by dllo on 16/5/8.
//  Copyright © 2016年 dllo. All rights reserved.
//

#import "JHJTabBarCollectionViewCell.h"

@interface JHJTabBarCollectionViewCell ()

@property (nonatomic, assign)BOOL isPlay;
@property (nonatomic, retain)CABasicAnimation *rotationAnimation;

@end

@implementation JHJTabBarCollectionViewCell

- (void)dealloc {
    
    [self removeObserver:self forKeyPath:@"isPlay"];
    [_myImageView release];
    [_myLabel release];
    [_rotationAnimation release];
    [_model release];
    [super dealloc];
}

- (void)setModel:(JHJTabBarModel *)model {
    if (_model != model) {
        [_model release];
        _model = [model retain];
    }
    self.myImageView.image = _model.image;
    self.isPlay = NO;
    [self rotationOrStop];
    self.myImageView.layer.timeOffset = 0;
}

- (instancetype)initWithFrame:(CGRect)frame {
    self = [super initWithFrame:frame];
    if (self) {
        [self creatView];
    }
    return self;
}

- (void)creatView {
    self.myImageView = [[UIImageView alloc] init];
    [self.contentView addSubview:self.myImageView];
    [_myImageView release];
    self.myImageView.userInteractionEnabled = YES;
    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapAction:)];
    [self.myImageView addGestureRecognizer:tap];
    [tap release];
    
}


- (void)tapAction:(UITapGestureRecognizer *)tap {
    if (!self.rotationAnimation) {
        CABasicAnimation *rotationAnimation = [CABasicAnimation animationWithKeyPath:@"transform.rotation.z"];
        rotationAnimation.duration = 2;
        rotationAnimation.fromValue = @(0);
        rotationAnimation.toValue = @(M_PI * 2);
        rotationAnimation.repeatCount = NSIntegerMax;
        rotationAnimation.cumulative = YES;
        self.rotationAnimation = rotationAnimation;
        [self.myImageView.layer addAnimation:rotationAnimation forKey:@"rotationAnimation"];
        [rotationAnimation release];
    }
    [self rotationOrStop];
}

- (void)rotation {
    CFTimeInterval stopTime = self.myImageView.layer.timeOffset;
    self.myImageView.layer.timeOffset = 0;
    // 设置速度为1.0;
    self.myImageView.layer.speed = 1.0;
    //重置开始时间.
    self.myImageView.layer.beginTime = 0;
    //        // 设置开始时间.
    // 开始时间 = 当前时间 - 偏移时间.
    self.myImageView.layer.beginTime = [self.myImageView.layer convertTime:CACurrentMediaTime() fromLayer:nil] - stopTime;
}

- (void)rotationOrStop {
    if (!self.isPlay) {
        
        [self stopRotation];
    } else {
        [self rotation];
    }
    self.isPlay = !self.isPlay;
}

- (void)stopRotation {
    // 每一个view 的layer层系统都记录了时间的属性, 通过改变view动画时间来控制动画效果.
    // 获得到当前旋转的时间点.
    CFTimeInterval stopTime = [self.myImageView.layer convertTime:CACurrentMediaTime() fromLayer:nil];
    // 设置动画的播放速度为0, 动画停止.
    self.myImageView.layer.speed = 0;
    // 记录当前的时间偏移量.
    self.myImageView.layer.timeOffset = stopTime;
    
}


- (void)layoutSubviews {
    [super layoutSubviews];
    self.myImageView.frame = CGRectMake(5, 5, 70, 70);
    self.myImageView.layer.cornerRadius = 35;
    self.myImageView.layer.masksToBounds = YES;
    
}



@end
